package com.lanou.web.controller;

import com.lanou.entity.ContentImg;
import com.lanou.entity.TbUser;
import com.lanou.service.IndexService;
import com.lanou.utils.ShiHuiResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController {
    @Autowired
    private IndexService indexService;


    @ApiOperation("类目展示")
    @GetMapping("/rest/itemcat/all")
    public Map<String,Object> listCat() throws IOException {
        Map<String,Object> catList = indexService.listCat();
        return catList;
    }

    @ApiOperation("首页轮播图展示")
    @GetMapping("/index")
    public List<ContentImg> showContentImg(){
        List<ContentImg> contentImgList = indexService.showContentImg();
        return contentImgList;
    }

    @ApiOperation("查看用户登录状态")
    @GetMapping("/user/{cookie}")
    public Map<String,Object> getUser(@PathVariable("cookie") String cookie){
        TbUser user = indexService.getUser(cookie);
        if(user != null){
            return ShiHuiResult.ok(user);
        }else {
            return ShiHuiResult.error(500,"用户未成功退出");
        }
    }

    @ApiOperation("查看用户退出状态")
    @GetMapping("/user/quit/{cookie}")
    public Map<String,Object> quitUser(@PathVariable("cookie") String cookie, HttpServletResponse response){
        TbUser user = indexService.quitUser(cookie);
        if(user != null){
            Cookie cookie1 = new Cookie("token", null);
            cookie1.setDomain("shihui.com");
            cookie1.setMaxAge(0);
            cookie1.setPath("/");
            response.addCookie(cookie1);
            return ShiHuiResult.ok(user);
        }else {
            return ShiHuiResult.error(500,"用户未成功退出");
        }
    }


}
